<?php

/**
 * Controller_ActivitysSubject
 * 学科活动
 */

class Controller_ActivitysSubject extends MyApp {

	public function __construct()
	{

		parent::__construct();

		$this->model->user   		= YOU::loadModel('user');
		$this->model->file   		= YOU::loadModel('file');
		$this->model->grade   		= YOU::loadModel('grade');
		$this->model->subject   	= YOU::loadModel('subject');
		$this->model->log   	    = YOU::loadModel('log');

	}
	/** 首页 **/
	public function actionIndex()
	{

	}

	/** 教学工作手册列表 **/
	public function actionActivitysSubjectList ()
	{	
		// 系统日记
		$a = $_GET['a'];
		$log_name = $this->user['user_name'];
		$this->model->log->add($a,$log_name);
		
		// 搜索
		$grade_id	= (int)$this->get->grade_id;
		$subject_id	= (int)$this->get->subject_id;
		$from		= $this->get->from;
		$to			= $this->get->to;

		$where = " ";

		if (!empty($grade_id)){
			$where .= " AND grade_id ='{$grade_id}' ";
			$this->tpl->assign("grade_id", $grade_id);
		}
		if (!empty($subject_id)){
			$where .= " AND subject_id ='{$subject_id}' ";
			$this->tpl->assign("subject_id", $subject_id);
		}
		if (!empty($from)){
			$where .= " AND create_time >='{$from}' ";
			$this->tpl->assign("from", $from);
		}
		if (!empty($to)){
			$where .= " AND create_time <='{$to}+1' ";
			$this->tpl->assign("to", $to);
		}
		// 年级
		$grade_list	   = $this->db->fetchAll("SELECT `grade_name`,`grade_id` FROM `tbl_grade_up` WHERE is_now=1");
		// 学科
		$subject_list  = $this->db->fetchAll("SELECT `subject_name`,`subject_id` FROM `tbl_subject`");

		// 分页
		YOU::loadLib('page', "", '0');
		$tmp = $this->db->fetchRow("SELECT COUNT(1) AS count FROM `tbl_activitys` WHERE activitys_type='1' {$where}");
		$count = $tmp['count'];
		$page_total = ceil($count/PAGE_LIMIT);
		// 当前页
		$p = intval($_GET['p']);
		if ($p == 0){
			$p  = 1;
		}
		if ($p > 1){
			$start = PAGE_LIMIT * ($p-1);
		}else{
			$start = 0;
		}
		// 1=学科活动
		$activitys_list = $this->db->fetchAll("SELECT * FROM `tbl_activitys` WHERE activitys_type='1' {$where} ORDER BY activitys_id DESC LIMIT {$start}, ".PAGE_LIMIT."");

        foreach($activitys_list as $key=>$value){

			// 年级
			$grade_data = $this->db->fetchRow("SELECT `grade_name` FROM `tbl_grade` WHERE grade_id={$value['grade_id']}");
			$activitys_list[$key]['grade_name'] = $grade_data['grade_name'];
			// 学科
			$subject_data = $this->db->fetchRow("SELECT `subject_name` FROM `tbl_subject` WHERE subject_id={$value['subject_id']}");
			$activitys_list[$key]['subject_name'] = $subject_data['subject_name'];
			// 转换时间
			$activitys_list[$key]['create_time'] = date('Y-m-d',strtotime($activitys_list[$key]['create_time']));
			// 提交者
			$activitys_list[$key]['user_name'] = $this->model->user->getNameById($value['user_id']);

		}

		$page = new Page(array('total' => $count, 'perpage' => PAGE_LIMIT));
		$page->next_page   = '下一页>';
		$page->pre_page    = '<上一页';
		$page->style_path  = "static/page_style";

		$this->tpl->assign("page", $page->show(4, "yahoo"));
		$this->tpl->assign("activitys_list", $activitys_list);
		$this->tpl->assign("grade_list", $grade_list);
		$this->tpl->assign("subject_list", $subject_list);

		$this->tpl->display('activitys/subject.list.tpl.php');
	}
	/** 学科活动添加 **/
	public function actionActivitysSubjectAdd()
	{
		// 系统日记
		$a = $_GET['a'];
		$log_name = $this->user['user_name'];
		$this->model->log->add($a,$log_name);
		
		// 全部年级
		$grade_list		= $this->db->fetchAll("SELECT `grade_name`,`grade_id` FROM `tbl_grade_up` WHERE is_now");
		// 全部学科
		$subject_list	= $this->db->fetchAll("SELECT `subject_name`,`subject_id` FROM `tbl_subject`");

		if ($_POST){

			$activitys_title        = $this->post->activitys_title;
			$grade_id   			= $this->post->grade_id;
			$subject_id   			= $this->post->subject_id;
			$activitys_con   		= $this->post->activitys_con;
			$activitys_award   		= $this->post->activitys_award;
			$activitys_summarize   	= $this->post->activitys_summarize;


			// 写入数据库
			$info = array(

			"activitys_title"  	    => $activitys_title,
			"grade_id"       		=> $grade_id,
			"subject_id"  			=> $subject_id,
			"activitys_con"       	=> $activitys_con,
			"activitys_award"  	    => $activitys_award,
			"activitys_summarize"   => $activitys_summarize,
			"user_id"   			=> $this->user['user_id'],
			// 学科活动 =1
			"activitys_type"       	=> 1,
			"status"                => 1,
			"create_time"           => date("Y-m-d H:i:s"),
			"update_time"           => "0000-00-00 00:00:00",

			);

			$this->db->do_insert("tbl_activitys", $info);
			$activitys_id = $this->db->insert_id();

			// 附件上传
			$file = $this->model->file->upload("activitys_file", $activitys_id, array("txt","ppt", "xls", "xlsx", "docs", "doc", "pdf", "jpg", "png", "gif", "zip", "rar"));

			if (!empty($file)){
				$info = array(
				"activitys_file" => $file['fname'],
				);

				$this->db->do_update("tbl_activitys", $info, "activitys_id='{$activitys_id}'");
			}
			Helper_Client::location("添加成功", "index.php?a=activityssubject.activityssubjectlist");

		}

		$this->tpl->assign("grade_list", $grade_list);
		$this->tpl->assign("subject_list", $subject_list);
		$this->tpl->display("activitys/subject.add.tpl.php");
	}
	/** 学科活动修改 **/
	public function actionActivitysSubjectUpdate()
	{
		// 系统日记
		$a = $_GET['a'];
		$log_name = $this->user['user_name'];
		$this->model->log->add($a,$log_name);
		
		$activitys_id	= (int)$this->get->activitys_id;
		// 全部年级
		$grade_list		= $this->db->fetchAll("SELECT `grade_name`,`grade_id` FROM `tbl_grade`");
		// 全部学科
		$subject_list	= $this->db->fetchAll("SELECT `subject_name`,`subject_id` FROM `tbl_subject`");

        // 学科活动
        $activitys_data	= $this->db->fetchRow("SELECT * FROM `tbl_activitys` WHERE `activitys_id`='{$activitys_id}' ");

        if (empty($activitys_data)){
			Helper_Client::alert("活动不存在");
		}

        $uid = $this->user['user_id'];
        if($activitys_data['user_id']  && ($uid !=$activitys_data['user_id']) ){
            Helper_Client::alert("对不起无此权限");
            exit;
        }

		if ($_POST){

			$activitys_title     	= $this->post->activitys_title;
			$grade_id   			= $this->post->grade_id;
			$subject_id   			= $this->post->subject_id;
			$activitys_con   		= $this->post->activitys_con;
			$activitys_award   		= $this->post->activitys_award;
			$activitys_summarize   	= $this->post->activitys_summarize;

			// 写入数据库
			$info = array(

			"activitys_title"  	    => $activitys_title,
			"grade_id"       		=> $grade_id,
			"subject_id"  			=> $subject_id,
			"activitys_con"       	=> $activitys_con,
			"activitys_award"  	    => $activitys_award,
			"activitys_summarize"   => $activitys_summarize,
			// 学科活动 =1
			"activitys_type"       	=> 1,
			"status"                => 1,
			"create_time"           => date("Y-m-d H:i:s"),
			"update_time"           => "0000-00-00 00:00:00",

			);

			$this->db->do_update("tbl_activitys", $info, "activitys_id='{$activitys_id}'");

// 附件上传
            if ($this->post->activitys_file_re=='1'){
                // 附件上传
                $file = $this->model->file->upload("activitys_file", $activitys_id, array("txt","ppt", "xls", "xlsx", "docs", "doc", "pdf", "jpg", "png", "gif", "zip", "rar"));

                if (!empty($file)){
                    $info = array(
                        "activitys_file" => $file['fname'],
                    );

                    $this->db->do_update("tbl_activitys", $info, "activitys_id='{$activitys_id}'");
                }
            }

			// 跳转
			Helper_Client::location("添加成功", "index.php?a=activityssubject.activityssubjectlist");

		}

		$this->tpl->assign("activitys_data", $activitys_data);
		$this->tpl->assign("subject_list", $subject_list);
		$this->tpl->assign("grade_list", $grade_list);
		$this->tpl->display("activitys/subject.update.tpl.php");
	}
	/** 学科活动删除 **/
	public function actionActivitysSubjectDel()
	{
		$activitys_id = $this->get->activitys_id;
        $activitys_data	= $this->db->fetchRow("SELECT * FROM `tbl_activitys` WHERE `activitys_id`='{$activitys_id}' ");

        if (empty($activitys_data)){
            Helper_Client::alert("活动不存在");
        }

        $uid = $this->user['user_id'];
        if($activitys_data['user_id']  && ($uid !=$activitys_data['user_id']) ){
            Helper_Client::alert("对不起无此权限");
            exit;
        }
		$this->db->query("DELETE  FROM `tbl_activitys` WHERE activitys_id='{$activitys_id}'");
		// 跳转
		Helper_Client::location("删除成功", "index.php?a=activityssubject.activityssubjectlist");
	}
	/** 教学工作手册列表 **/
	public function actionParticular ()
	{
		// 系统日记
		$a = $_GET['a'];
		$log_name = $this->user['user_name'];
		$this->model->log->add($a,$log_name);
		
		$activitys_id = $this->get->activitys_id;
		//学科活动
		$activitys_data	= $this->db->fetchRow("SELECT * FROM `tbl_activitys` WHERE `activitys_id`='{$activitys_id}' ");
		// 提交者
		$activitys_data['user_name'] = $this->model->user->getNameById($activitys_data['user_id']);
		// 年级名称
		$row = $this->model->grade->getInfo($activitys_data['grade_id']);
		$activitys_data['grade_name'] = $row['grade_name'];
		// 学科名称
		$row = $this->model->subject->getInfo($activitys_data['subject_id']);
		$activitys_data['subject_name'] = $row['subject_name'];
		
		$this->tpl->assign("activitys_data", $activitys_data);
		$this->tpl->display('activitys/particular.tpl.php');
	}
	
	/** 下载**/
		public function actionoutput(){
			$fname = $this->get->activitys_file;
			$fpath = UPLOAD_PATH."/activitys_file/{$fname}";

			// 输出下载文件头s
			header("Content-Disposition: attachment; filename={$fname}");
			// 在线输出
			readfile($fpath);

		}
	
}
?>